class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0;
        int right = 0;
        int len = 0;
        int zero = 0;
        while (right < nums.size())
        {
            if (nums[right] == 1)
            {
                right++;
            }
            else if (nums[right] == 0)
            {
                zero++;
                if (zero > k)
                {
                    while (zero > k)
                    {
                        if (nums[left] == 1)
                        {
                            left++;
                        }
                        else if (nums[left] == 0)
                        {
                            zero--;
                            left++;
                        }
                    }
                }
                right++;
            }
            len = len > right - left ? len : right - left;
        }

        return len;
    }
};